<template>
  <!-- 盘点记录 -->
  <div>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-form :inline="true" :model="queryParam" class="demo-form-inline" size="medium">
        <el-form-item label="单号">
          <el-input v-model="queryParam.billNumber" placeholder="请输入单号" clearable></el-input>
        </el-form-item>
        <el-form-item label="盘点日期">
          <el-date-picker v-model="inventoryDate" type="daterange" range-separator="至" clearable
            start-placeholder="开始日期" end-placeholder="结束日期" @change="handleDateChange">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="rest">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-table :data="tableData" border style="width: 100%" height="58vh">
        <el-table-column type="index" label="序号" align="center" width="50"> </el-table-column>
        <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
          :width="item.width">
          <template #default="scope">
            <span v-if="item.prop === 'labelType'">
              <el-tag v-show="scope.row.labelType === 0">档案盘点</el-tag>
              <el-tag v-show="scope.row.labelType === 1">盒盘点</el-tag>
              <el-tag v-show="scope.row.labelType === 2">混合盘点</el-tag>
            </span>
            <span v-else-if="item.prop === 'inventoryStatus'">
              <el-tag v-show="scope.row.inventoryStatus === 0" type="danger">未盘点</el-tag>
              <el-tag v-show="scope.row.inventoryStatus === 1" type="success">盘点完成</el-tag>
            </span>
            <span v-else-if="item.prop === 'inventoryResult'">
              <el-tag v-show="scope.row.inventoryResult === 0" type="success">正常</el-tag>
              <el-tag v-show="scope.row.inventoryResult === 1" type="danger">异常</el-tag>
            </span>
            <span v-else-if="item.prop === 'inventoryTool'">
              <el-tag v-if="scope.row.inventoryTool === 0">人工</el-tag>
              <el-tag v-if="scope.row.inventoryTool === 1">手持机</el-tag>
              <el-tag v-if="scope.row.inventoryTool === 2">盘点车</el-tag>
              <el-tag v-if="scope.row.inventoryTool === 3">密集架</el-tag>
            </span>
            <span v-else>{{ scope.row[item.prop] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="100">
          <template slot-scope="{ row }">
            <el-button icon="el-icon-view" link size="mini" @click="handleShow(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <pagination ref="pagination" :totalCount="totalCount" @pagination="pagination"></pagination>
    </el-card>
    <inventory-bill-dialog ref="inventoryBillDialog" />
  </div>
</template>

<script>
import { pageList } from '@/api/rfid/inventoryBill'
import inventoryBillDialog from './dialog/inventoryBillDialog.vue'
import moment from 'moment/moment'
export default {
  name: 'inventoryBill',
  components: { inventoryBillDialog },
  data() {
    return {
      totalCount: 0, //总数
      //查询条件
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      queryParam: {
        inventoryStatus: 1,
      },
      inventoryDate: [],
      tableData: [],
      tableProps: [
        { title: '盘点单号', prop: 'billNumber', width: '', checked: true },
        { title: '盘点主题', prop: 'billTitle', width: '', checked: true },
        { title: '盘点类型', prop: 'labelType', width: '', checked: true },
        { title: '盘点工具', prop: 'inventoryTool', width: '', checked: true },
        { title: '盘点人', prop: 'inventoryBy', width: '', checked: true },
        { title: '盘点日期', prop: 'inventoryDate', width: '', checked: true },
        { title: '盘点状态', prop: 'inventoryStatus', width: '', checked: true },
        { title: '盘点结果', prop: 'inventoryResult', width: '', checked: true },
      ],
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    handleDateChange(value) {
      if (value === null) {
        this.queryParam.beginTime = null
        this.queryParam.endTime = null
      } else {
        this.queryParam.beginTime = moment(value[0]).format('YYYY-MM-DD')
        this.queryParam.endTime = moment(value[1]).format('YYYY-MM-DD')
      }
    },
    onSubmit() {
      this.$refs.pagination.resetPagination()
    },
    rest() {
      this.inventoryDate = []
      this.queryParam = {
        inventoryStatus: 1,
      }
      this.$refs.pagination.resetPagination()
    },
    loadData() {
      var param = Object.assign(this.queryParam, this.searchModel)
      pageList(param)
        .then((res) => {
          if (res.data.code === 200) {
            this.tableData = res.data.data.records
            this.totalCount = res.data.data.total
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    pagination(val) {
      this.searchModel.pageNo = val.page
      this.searchModel.pageSize = val.limit
      this.loadData()
    },
    handleShow(row) {
      this.$refs.inventoryBillDialog.show(row)
    },
  },
}
</script>

<style scoped lang="less">
.demo-form-inline {
  .el-form-item {
    margin-bottom: 0 !important;
  }
}
</style>
